<template>
  <div style="margin:20px">
    <el-form :inline="true" :model="searchRecord" :rules="searchRules">
      <el-form-item label="资源名" prop="resourceName">
        <el-input v-model="searchRecord.resourceName" placeholder="资源名" />
      </el-form-item>
      <el-form-item label="资源编码" prop="resourceCode">
        <el-input v-model="searchRecord.resourceCode" placeholder="资源编码" />
      </el-form-item>
      <el-form-item label="资源链接" prop="resourceLink">
        <el-input v-model="searchRecord.resourceLink" placeholder="资源链接" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row style="margin-bottom: 10px">
      <el-button type="primary" size="small" @click="create">新增</el-button>
      <el-button type="primary" size="small" @click="bathDelete">批量删除</el-button>
      <!--      <el-button type="primary" size="small" @click="templateDown">模板</el-button>-->
      <!--      <el-button type="primary" size="small" @click="importData">导入</el-button>-->
      <!--      <el-button type="primary" size="small" @click="exportData">导出</el-button>-->
    </el-row>
    <el-row>
      <el-table
        ref="mainTable"
        :data="tableData"
        :stripe="true"
        :border="true"
        :highlight-current-row="true"
        style="width: 100%"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        row-key="resourceId"
        @select="tableSelect"
        @selection-change="tableSelectionChange"
      >
        <el-table-column type="selection" align="center" width="55" />
        <el-table-column prop="resourceId" label="资源ID" align="center" />
        <el-table-column prop="resourceName" label="资源名" align="center" />
        <el-table-column prop="resourceCode" label="资源编码" align="center" />
        <el-table-column prop="resourceLink" label="资源链接" align="center" />
        <el-table-column prop="resourceIcon" label="资源图标" align="center" />
        <el-table-column prop="resourceSort" label="资源排序号" align="center" />
        <el-table-column fixed="right" label="操作" align="center" width="150">
          <template v-slot:default="scope">
            <el-button type="text" size="small" @click="query(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="remove(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--      <el-pagination-->
      <!--        style="margin-top: 10px"-->
      <!--        :current-page="page.currentPage"-->
      <!--        :layout="$store.state.page.layout"-->
      <!--        :page-sizes="$store.state.page.pageSizes"-->
      <!--        :page-size="page.pageSize"-->
      <!--        :total="page.total"-->
      <!--        @size-change="pageSizesChange"-->
      <!--        @current-change="currentPageChange"-->
      <!--      />-->
    </el-row>
    <!--    新建 -->
    <el-dialog ref="createDialog" :visible.sync="createDialog.visible" title="新增" width="30%" :before-close="done=>done()">
      <template slot>
        <el-form ref="createDialogForm" :model="createRecord" :rules="createRules" label-width="80px" label-position="right">
          <el-form-item label="资源名" prop="resourceName">
            <el-input v-model="createRecord.resourceName" placeholder="资源名" />
          </el-form-item>
          <el-form-item label="资源编码" prop="resourceCode">
            <el-input v-model="createRecord.resourceCode" placeholder="资源编码" />
          </el-form-item>
          <el-form-item label="资源链接" prop="resourceLink">
            <el-input v-model="createRecord.resourceLink" placeholder="资源链接" />
          </el-form-item>
          <el-form-item label="资源图标" prop="resourceIcon">
            <el-input v-model="createRecord.resourceIcon" placeholder="资源图标" @click.native="selectIcon" />
          </el-form-item>
          <el-form-item label="资源排序号" prop="resourceSort">
            <el-input v-model="createRecord.resourceSort" placeholder="资源排序号" />
          </el-form-item>
        </el-form>
      </template>
      <template slot="footer" class="dialog-footer">
        <el-button @click="createDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="createDialogSave">确 定</el-button>
      </template>
    </el-dialog>
    <!--    修改 -->
    <el-dialog ref="editDialog" :visible.sync="editDialog.visible" title="编辑" width="40%" :before-close="done=>done()">
      <template v-slot:default class="dialog-title">
        <el-form ref="editDialogForm" :model="editRecord" :rules="editRules" label-width="80px" label-position="right">
          <el-form-item label="资源名" prop="resourceName">
            <el-input v-model="editRecord.resourceName" placeholder="资源名" />
          </el-form-item>
          <el-form-item label="资源编码" prop="resourceCode">
            <el-input v-model="editRecord.resourceCode" placeholder="资源编码" />
          </el-form-item>
          <el-form-item label="资源链接" prop="resourceLink">
            <el-input v-model="editRecord.resourceLink" placeholder="资源链接" />
          </el-form-item>
          <el-form-item label="资源图标" prop="resourceIcon">
            <el-input v-model="editRecord.resourceIcon" placeholder="资源图标" @click.native="selectIcon" />
          </el-form-item>
          <el-form-item label="资源排序号" prop="resourceSort">
            <el-input v-model="editRecord.resourceSort" placeholder="资源排序号" />
          </el-form-item>
        </el-form>
      </template>
      <template v-slot:footer class="dialog-footer">
        <el-button @click="editDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogSave">确 定</el-button>
      </template>
    </el-dialog>
    <!--  查看 -->
    <el-dialog ref="queryDialog" :visible.sync="queryDialog.visible" title="查看" width="30%" :before-close="done=>done()">
      <template v-slot:default class="dialog-title">
        <el-form :model="queryRecord" label-width="80px" label-position="right">
          <el-form-item label="资源名" prop="resourceName">
            <el-input v-model="queryRecord.resourceName" placeholder="资源名" :readonly="true" />
          </el-form-item>
          <el-form-item label="资源编码" prop="resourceCode">
            <el-input v-model="queryRecord.resourceCode" placeholder="资源编码" :readonly="true" />
          </el-form-item>
          <el-form-item label="资源链接" prop="resourceLink">
            <el-input v-model="queryRecord.resourceLink" placeholder="资源链接" :readonly="true" />
          </el-form-item>
          <el-form-item label="资源图标" prop="resourceIcon">
            <el-input v-model="queryRecord.resourceIcon" placeholder="资源图标" :readonly="true" />
          </el-form-item>
          <el-form-item label="资源排序号" prop="resourceSort">
            <el-input v-model="queryRecord.resourceSort" placeholder="资源排序号" :readonly="true" />
          </el-form-item>
        </el-form>
      </template>
    </el-dialog>

    <el-dialog ref="iconDialog" :visible.sync="iconDialog.visible" title="菜单图标选择" width="80%" :before-close="done=>done()">
      <template v-slot:default class="dialog-title">
        <el-tabs v-model="iconTabsActive" type="border-card">
          <el-tab-pane label="Icons" name="icons">
            <div v-for="item of svgIcons" :key="item" @click="handleClipboard(item,$event)">
              <div class="icon-item">
                <svg-icon :icon-class="item" class-name="disabled" />
                <span>{{ item }}</span>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="Element-UI Icons" name="elementUI">
            <div v-for="item of elementIcons" :key="item" @click="handleClipboard(item,$event)">
              <div class="icon-item">
                <i :class="'el-icon-' + item" />
                <span>{{ item }}</span>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import { create, edit, deleteByIds, findAll } from '@/api/resource'
import { array2tree, removeEmptyString } from '@/utils/function'
import { Message } from 'element-ui'
import svgIcons from '@/utils/svg-icons'
import elementIcons from '@/utils/element-icons'
export default {
  name: 'Resource',
  data() {
    return {
      // 搜索表单
      searchRecord: {
        resourceName: '',
        resourceCode: '',
        resourceLink: '',
        resourceIcon: '',
        resourceSort: 0
      },
      searchRules: {
        resourceName: [],
        resourceCode: [],
        resourceLink: [],
        resourceIcon: [],
        resourceSort: []
      },
      resourceList: [],
      tableData: [], // 表格数据
      // // 分页参数
      // page: {
      //   currentPage: 1,
      //   pageSize: 10,
      //   total: 0
      // },
      tableSelection: [],
      createDialog: {
        visible: false
      },
      editDialog: {
        visible: false
      },
      queryDialog: {
        visible: false
      },
      createRecord: {
        resourceName: '',
        resourceCode: '',
        resourceLink: '',
        resourceIcon: '',
        resourceSort: 0,
        parentId: null
      },
      createRules: {
        resourceName: [{ type: 'string', required: true, message: '用户名是必填项,且长度小于', trigger: 'blur' }],
        resourceCode: [{ type: 'string', required: true, message: '密码是必填项', trigger: 'blur' }],
        resourceLink: [],
        resourceIcon: [],
        resourceSort: []
      },
      editRecord: {
        resourceName: '',
        resourceCode: '',
        resourceLink: '',
        resourceIcon: '',
        resourceSort: 0
      },
      editRules: {
        resourceName: [{ type: 'string', message: '用户名是必填项,且长度小于', trigger: 'blur' }],
        resourceCode: [{ type: 'string', message: '密码是必填项', trigger: 'blur' }],
        resourceLink: [],
        resourceIcon: [],
        resourceSort: []
      },
      queryRecord: {
        resourceName: '',
        resourceCode: '',
        resourceLink: '',
        resourceIcon: '',
        resourceSort: 0
      },
      iconTabsActive: 'icons',
      svgIcons,
      elementIcons,
      iconDialog: {
        visible: false
      }

    }
  },
  created() {
    this.getList()
  },
  methods: {
    tableSelect(selection, row) {
      // this.tableSelection = selection
    },
    tableSelectionChange(selection) {
      this.tableSelection = selection
    },
    // 渲染表格
    getList() {
      findAll().then(result => {
        this.resourceList = result.data
        this.tableData = array2tree(result.data, 'resourceId', 'parentId', 0)
      }).catch(e => {

      })
    },
    // // 分页相关函数
    // pageSizesChange(val) {
    //   this.page.pageSize = val
    //   this.getList()
    // },
    // currentPageChange(val) {
    //   this.page.currentPage = val
    //   this.getList()
    // },
    query(record) {
      this.queryDialog.visible = true
      this.queryRecord = record
    },
    edit(record) {
      this.editDialog.visible = true
      this.editRecord = record
    },
    editDialogSave() {
      this.$refs.editDialogForm.validate(valid => {
        if (valid) {
          removeEmptyString(this.editRecord)
          edit(this.editRecord).then(result => {
            this.editDialog.visible = false
            this.getList()
            Message.success('编辑成功！！！')
          }).catch(e => { })
        } else {
          return false
        }
      })
    },
    remove(record) {
      this.$confirm('确认删除？')
        .then(() => {
          deleteByIds([record.userId]).then(result => {
            this.getList()
            Message.success('删除成功！！！')
          }).catch(e => {

          })
        }).catch(() => {})
    },
    create() {
      this.createDialog.visible = true
    },
    createDialogSave() {
      if (this.tableSelection.length > 1) {
        Message.info('至多选择一个，否者无法增加资源！！！')
        return
      }
      if (this.tableSelection.length === 0) {
        // 这是增加顶层菜单的 menu
        this.createRecord.parentId = 0
      } else {
        this.createRecord.parentId = this.tableSelection[0].resourceId
      }
      this.$refs.createDialogForm.validate((valid) => {
        if (valid) {
          removeEmptyString(this.createRecord)
          create(this.createRecord).then(result => {
            this.createDialog.visible = false
            this.getList()
            this.createRecord = {
              resourceName: '',
              resourceCode: '',
              resourceLink: '',
              resourceIcon: '',
              resourceSort: 0,
              parentId: null
            }
            Message.success('新增成功！！！')
          }).catch(e => {

          })
        } else {
          return false
        }
      })
    },
    bathDelete() {
      if (this.tableSelection.length <= 0) {
        Message.info('请至少选择一个')
        return
      }
      this.$confirm('确认删除？')
        .then(() => {
          const ids = this.tableSelection.map(record => record.userId)
          deleteByIds(ids).then(result => {
            this.getList()
            Message.success('删除成功！！！')
          }).catch(e => {

          })
        }).catch(() => {})
    },
    importData() {

    },
    exportData() {

    },
    templateDown() {

    },
    selectIcon() {
      this.iconDialog.visible = true
    },
    handleClipboard(text, event) {
      // clipboard(text, event)
      if (this.iconTabsActive === 'elementUI') {
        text = 'el-icon-' + text
      }
      if (this.createDialog.visible) {
        this.createRecord.resourceIcon = text
      }
      if (this.editDialog.visible) {
        this.editRecord.resourceIcon = text
      }
      this.iconDialog.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.icon-item {
  margin: 20px;
  height: 85px;
  text-align: center;
  width: 100px;
  float: left;
  font-size: 30px;
  color: #24292e;
  cursor: pointer;
}
.icon-item span {
  display: block;
  font-size: 16px;
  margin-top: 10px;
}
.icon-item.disabled {
  pointer-events: none;
}

</style>
